<template>
  <div id="user_app">
    <div id="role_control">
      <el-button type="danger" plain round @click="isShow.flag = true"
        >新增</el-button
      >
    </div>
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="账号名称"> </el-table-column>
      <el-table-column prop="rolename" label="角色名称"></el-table-column>
      <el-table-column prop="status" label="角色状态">
        <template v-slot="hhh">
          <el-button
            type="success"
            size="mini"
            plain
            round
            v-if="hhh.row.status == 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" plain round v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template v-slot="$props">
          <el-button
            type="warning"
            size="mini"
            plain
            round
            @click="edit_role($props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            plain
            round
            @click="del_role($props.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="pagintor.size"
      @current-change='take_page'
    >
    </el-pagination>
    <Dialog :isShow="isShow" @reload="reload" ref='dialog_ref'/>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import Dialog from "../components/AddUser.vue";
export default {
  data() {
    return {
      pagintor: { page: 1, size: 3 },
      isShow: { flag: false },
    };
  },
  components: {
    Dialog,
  },
  computed: {
    ...mapState("user_model", ["userList", "count"]),
  },
  methods: {
      edit_role(obj){
          this.isShow.flag=true
          this.$refs.dialog_ref.setData(obj)
      },
      del_role(id){
          this.$axios.post('/userdelete',{id}).then(res=>{
              if(res.code==200){
                  this.$message({
                      type:"success",
                      message:"删除成功"
                  })
                  this.isShow.flag = false
              }
          })
      },
      take_page(page){
            this.pagintor.page = page
            this.userListActions(this.pagintor)
            console.log(this.$store.state.user_model)
      },
    reload() {
      this.userListActions(this.pagintor);
      this.userCountActions();
    },
    ...mapActions("user_model", ["userListActions", "userCountActions"]),
  },
  created() {
    this.userListActions(this.pagintor);
    this.userCountActions();
  },
};
</script>

<style>
#role_control {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}

.el-table__header thead,
.el-table__header thead th {
  background-color: #ff674b;
  color: #fff;
  line-height: 40px;
  border: 0;
}
</style>